﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Cat.Helper;

using System.Text.RegularExpressions;
using System.IO;


namespace PicServer.Konachan
{
    class Start
    {
        int page_count = 0;
       
        public void Scanner() {
            if (page_count < 15)
            {
                Console.WriteLine("当前运行扫描{0}页", page_count);
                new Web_helper().HttpGetAsync(string.Format(Konachan.Konachan_value.index_url, page_count++), List_complete);
            }
            else {
                Console.WriteLine("列表读取完成，启动单页扫描。 当前任务总数：{0}个",Konachan_value.unexecuted.Count);
              Content_reader();
            }
        }

        private void Content_reader()
        {
            if (Konachan_value.unexecuted.Count > 0)
            {
                String local_url = Konachan_value.unexecuted.Dequeue();
                new Web_helper().HttpGetAsync("http://konachan.net" +local_url, Content_complete);
            }
            else {
                Console.WriteLine("解析完成，准备调用下载");
                File_download();
            }
        }

        private void File_download()
        {
            if (Konachan_value.Img_list.Count > 0)
            {

                string url = Konachan_value.Img_list.Dequeue();

                string filepath = Static_value.Konach_path + DateTime.Now.ToString("yyyyMMdd") + "\\";
         
                if (new Web_helper().FileDownloader(url, filepath))
                {
                    Console.WriteLine("下载成功");
                    new File_helper().Log_Update(Static_value.tag_path + "old_url.txt", Get_ImgID(url));
                    Static_value.old_url.Add(Get_ImgID(url));

              
                }
                else {
                    Console.WriteLine("下载失败");
                }
                File_download();

            }
            else {
                Console.WriteLine("全部下载完成");
            }
         

             
        }

  
        /// <summary>
        /// 从文章页面提取链接
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Content_complete(object sender, OpenReadCompletedEventArgs e)
        {
            String shtml = Html_helper.get_html(e);
            String img_url = "https:" + new Html_helper().Get_href(shtml, "//a[@id='highres-show']")[0];

            string Img_id = Get_ImgID(img_url);
            Console.WriteLine("Img_id" + Img_id);
            if (!Static_value.old_url.Contains(Img_id))
            {
                Konachan_value.Img_list.Enqueue(img_url);
            }
            else {
                Console.WriteLine("已经扫描过 拒绝添加");
            }
            Content_reader();
        }

        private static string Get_ImgID(string img_url)
        {
            string Img_id = Regex.Match(img_url, "/Konachan.com%20-%[0-9]{1,10}%20").ToString();

            Img_id = Regex.Match(img_url, "%[0-9]{1,11}%").ToString();
       
            Img_id = Img_id.Replace("%20", "");
            Img_id = Img_id.Replace("%", "");
            return Img_id;
        }

        private void List_complete(object sender, OpenReadCompletedEventArgs e)
        {
            String shtml = Html_helper.get_html(e);
            foreach (String line in new Html_helper().Get_href(shtml, "//a[@class='thumb']")) {


                String post_url = Regex.Match(line, "/post/show/[0-9]{1,9}/").ToString(); ;
                post_url = Regex.Match(post_url, "/[0-9]{1,9}/").ToString().Replace("/", "");
                Console.WriteLine("当前页面ID："+ post_url);
                if (!Static_value.old_url.Contains(post_url))
                    Konachan_value.unexecuted.Enqueue(line);
                else
                    Console.WriteLine("页面扫描过，拒绝添加");
            }
              
            Scanner();
        }
    }
}
